//
//  ZYXZYXMyOrderVC.m
//  RuiBeiKang
//
//  Created by iOS开发1 on 2019/1/10.
//  Copyright © 2019年 JW. All rights reserved.
//

#import "ZYXZYXMyOrderVC.h"

#import "ZYXMyOrderCell.h"
#import "ZYXMyOrderModel.h"

#import "ZYXVideoPlayVC.h"
@interface ZYXZYXMyOrderVC ()
///页数
@property (strong,nonatomic) NSString *page;
@property (strong,nonatomic) NSMutableArray *dataArr;
@end

@implementation ZYXZYXMyOrderVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"购买记录";

}
#pragma mark MainUI
-(void)setupViews{
    self.tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavigationBar_Height);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"ZYXMyOrderCell" bundle:nil] forCellReuseIdentifier:@"ZYXMyOrderCell"];
    [self.view addSubview:self.tableView];
    
    [self setupRefresh];
    self.page = @"1";
    [self loadRequestPage:@"1"];
    
}
#pragma mark 加载数据
- (void)setupRefresh {
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = @"1";
        [self.dataArr removeAllObjects];
        [self loadRequestPage:self.page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page = SF(@"%ld",[self.page integerValue] + 1);
        [self loadRequestPage:self.page];
    }];
    /// 设置contentInset 补缺
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = HomeIndicator_Height;
}
- (void)endRefresh {
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
}
- (void)loadRequestPage:(NSString *)page {
    [NetWork POSTWithUrlString:kInterfaceAllUrl(PATH_WDMember_Recode) parmas:@{@"user_id":UID,@"page":self.page} successBloock:^(NSDictionary *responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] isEqualToNumber:@200]) {
            
            if ([self.page integerValue] <= [responseObject[@"data"][@"last_page"] integerValue]) {
                
                NSArray *modelArr = [ZYXMyOrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"data"]];
                [self.dataArr addObjectsFromArray:modelArr];
              
                
            }else{
                [JKToast showWithText:@"暂无加载更多内容"];
            }
           
            
            
        }else{
            [JKToast showWithText:responseObject[@"message"]];
        }
        [self.tableView reloadData];
        [self endRefresh];
            
        
        
        
    } failureBlock:^(NSError *error) {
        
    }];
    
}
#pragma mark tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
   
    ZYXMyOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZYXMyOrderCell"];
    cell.model = self.dataArr[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  HEIGHT(138);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return HEIGHT(8);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击");
     ZYXMyOrderModel *model  = self.dataArr[indexPath.section];
    if ([model.type_id isEqualToString:@"2"]) {
        ZYXVideoPlayVC *vc = [[ZYXVideoPlayVC alloc]init];
        vc.title = model.title;
        vc.zid = model.goods_id;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}




@end
